# I wouldn't actually do this in Ruby but just for comparison
require "mscorlib"
include System

class SimpleDependencyResolver
  def initialize()
    @registry = {}
  end

  def resolve(key)
    @registry[key]
  end
  
  def register(key, value)
    @registry[key] = value
  end
end

class InversionOfControlContainer
  class << self
    def set_resolver(resolver)
      @@resolver = resolver
    end
    
    def resolve(key)
      @@resolver.resolve(key)
    end
  end
end

class BlueWhitePalette
  def background_color
    ConsoleColor.blue
  end
  
  def foreground_color
    ConsoleColor.white
  end
end

class RedGreenPalette
  def background_color
    ConsoleColor.red
  end
  
  def foreground_color
    ConsoleColor.green
  end
end

class SimpleMessager
  def initialize(message)
    @message = message
  end
  
  def message
    @message
  end
end

class ConsoleWriter
  def initialize(args={})
    @palette = args[:palette] || InversionOfControlContainer.resolve(:palette)
    @messager = args[:messager] || InversionOfControlContainer.resolve(:messager)
  end
  
  def write
    Console.background_color = @palette.background_color
    Console.foreground_color = @palette.foreground_color
    puts @messager.message
  end
end

resolver = SimpleDependencyResolver.new
resolver.register(:palette, BlueWhitePalette.new)
resolver.register(:messager, SimpleMessager.new("Dependency Injection!"))
InversionOfControlContainer.set_resolver(resolver)

writer = ConsoleWriter.new
writer.write

# Change dependencies on-the-fly
resolver.register(:palette, RedGreenPalette.new)
writer = ConsoleWriter.new
writer.write

Console.reset_color
